#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define ABS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
#define EQ(a, b) (ABS(a, b) < 1e-6)

typedef struct {
    int len;
    double data[4];
} List;

void show(List in)
{
    for (int i = 0; i < in.len; i++) {
        printf("%.2lf%c", in.data[i], i == in.len - 1 ? '\n' : ' ');
    }
}

double rmv(List *in, int i)
{
    double result = in->data[i];
    memmove(in->data + i, in->data + i + 1, (in->len - i - 1) * sizeof(in->data[0]));
    in->len--;
    return result;
}

List add(List in, double a)
{
    in.data[in.len] = a;
    in.len++;
    return in;
}

bool check(List in)
{
    if (in.len == 1) {
        return EQ(in.data[0], 24.0);
    }

    for (int i = 0; i < in.len; i++) {
        for (int j = 0; j < in.len; j++) {
            if (j <= i) {
                continue;
            }

            List tmp = in;
            double b = rmv(&tmp, j);
            double a = rmv(&tmp, i);
            if (check(add(tmp, a + b)) ||
                check(add(tmp, a - b)) ||
                check(add(tmp, b - a)) ||
                check(add(tmp, a * b)) ||
                (b != 0 && check(add(tmp, a / b))) ||
                (a != 0 && check(add(tmp, b / a)))) {
                return true;
            }
        }
    }
    return false;
}

bool judgePoint24(int *nums, int numsSize)
{
    List in = { numsSize, (double) nums[0], (double) nums[1], (double) nums[2], (double) nums[3] };
    return check(in);
}

int main(int argc, char const *argv[])
{
    int num = 4;
    int nums[] = { 4, 1, 8, 7 };
    printf("%d\n", judgePoint24(nums, num));
    return 0;
}
